package com.crm.backend.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.backend.domain.entity.RestBean;
import com.crm.backend.domain.po.Services;
import com.crm.backend.service.IServicesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dyy
 * @since 2025-05-29
 */
@RestController
@RequestMapping("/api/v1/services")
public class ServicesController {

    @Autowired
    private IServicesService servicesService;

    /**
     * 创建服务单据
     * @param services 服务单据信息
     * @return RestBean封装的响应结果
     */
    @PostMapping
    public RestBean<Services> createService(@RequestBody Services services) {
        boolean savedService = servicesService.save(services);
        return RestBean.success();
    }

    /**
     * 分配服务单据
     * @param id 服务单据编号
     * @param assignee 分配给的对象
     * @param assignTime 分配时间
     * @return RestBean封装的响应结果
     */
//    @PutMapping("/{id}/assign")
//    public RestBean<Services> assignService(@PathVariable Integer id,
//                                            @RequestParam String assignee,
//                                            @RequestParam LocalDateTime assignTime) {
//        Services service = servicesService.getById(id);
//        if (service != null) {
//            service.setAssignee(assignee);
//            service.setAssignTime(assignTime);
//            service.setStatus(2); // 设置状态为“已分配”
//            Services updatedService = servicesService.updateById(service);
//            return RestBean.success(updatedService);
//        } else {
//            return RestBean.failure(404, "服务单据未找到");
//        }
//    }

    /**
     * 处理服务单据
     * @param id 服务单据编号
     * @param handleMethod 处理方法
     * @param handleTime 处理时间
     * @return RestBean封装的响应结果
     */
//    @PutMapping("/{id}/handle")
//    public RestBean<Services> handleService(@PathVariable Integer id,
//                                            @RequestParam String handleMethod,
//                                            @RequestParam LocalDateTime handleTime) {
//        Services service = servicesService.getById(id);
//        if (service != null) {
//            service.setHandleMethod(handleMethod);
//            service.setHandleTime(handleTime);
//            service.setStatus(3); // 设置状态为“已处理”
//            Services updatedService = servicesService.updateById(service);
//            return RestBean.success(updatedService);
//        } else {
//            return RestBean.failure(404, "服务单据未找到");
//        }
//    }

    /**
     * 提交服务反馈
     * @param id 服务单据编号
     * @param customerFeedback 客户反馈
     * @param customerSatisfaction 客户满意度
     * @param feedbackTime 反馈时间
     * @return RestBean封装的响应结果
     */
    @PutMapping("/{id}/feedback")
    public RestBean<Services> feedbackService(@PathVariable(value = "id") Integer id,
                                              @RequestParam(value = "customerFeedback") String customerFeedback,
                                              @RequestParam(value = "customerSatisfaction") Integer customerSatisfaction,
                                              @RequestParam(value = "feedbackTime") String feedbackTime) {
        Services service = servicesService.getById(id);
        if (service != null) {
            service.setSerCusfeedback(customerFeedback);
            service.setSerCussatisfac(String.valueOf(customerSatisfaction));
            service.setSerFeedbackdate(LocalDate.parse(feedbackTime));
            if (customerSatisfaction >= 3) {
                service.setSerState("4"); // 设置状态为“已归档”
            } else {
                service.setSerState("2"); // 设置状态为“已分配”，重新处理
            }
            boolean updatedService = servicesService.updateById(service);
            return RestBean.success();
        } else {
            return RestBean.failure(404, "服务单据未找到");
        }
    }

    /**
     * 查询归档服务单据
     * @param serState 服务单据状态
     * @param page 页码
     * @param size 每页大小
     * @return RestBean封装的响应结果
     */
    @GetMapping("/page")
    public RestBean<IPage<Services>> getServicesByPage(
            @RequestParam(value = "serState", required = false) Integer serState,
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "size") Integer size) {
        // 创建分页查询对象
        Page<Services> pageQuery = new Page<>(page, size);
        // 创建查询条件
        LambdaQueryWrapper<Services> queryWrapper = new LambdaQueryWrapper<>();
        // 如果状态不为空，则添加状态过滤条件
        if (serState != null) {
            queryWrapper.eq(Services::getSerState, serState);
        }
        // 执行分页查询
        IPage<Services> servicePage = servicesService.page(pageQuery, queryWrapper);
        // 封装分页结果并返回
        return RestBean.success(servicePage);
    }
}